<?php
/**
 * 首页模块
 * @author Yangdong Zhang <zhangyangdong@gouuse.cn>
 * @time 2018/10/12 15:47
 */
namespace App\Controller;

use App\Model\Note;
use App\Util\EditorMdUploader;

class Index extends Base
{
    /**
     * Index constructor.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 首页
     * @throws \ErrorException
     */
    public function index()
    {
        $this->display('index');
    }

    /**
     * @throws \ErrorException
     */
    public function detail()
    {
        $this->display('detail');
    }

    /**
     * 笔记记录
     */
    public function note()
    {
        $action = $_REQUEST['action'];
        $model  = new Note();
        $model->setUserId($this->tokenInfo->user_id);
        $data   = $model->$action();
        exit(json_encode($data, JSON_UNESCAPED_UNICODE));
    }

    /**
     * 上传图片
     */
    public function upload()
    {
        $savePath = ROOT_PATH . '/resources/uploads/';
        $saveURL  = url() . '/resources/uploads/';

        $formats  = ['image' => ['gif', 'jpg', 'jpeg', 'png', 'bmp']];

        $name = 'editormd-image-file';

        if (isset($_FILES[$name])) {
            $imageUploader = new EditorMdUploader($savePath, $saveURL, $formats['image'], 1);  // Ymdhis表示按日期生成文件名，利用date()函数

            $imageUploader->config([
                'maxSize' => 8192,        // 允许上传的最大文件大小，以KB为单位，默认值为1024
                'cover'   => true         // 是否覆盖同名文件，默认为true
            ]);

            if ($imageUploader->upload($name)) {
                $imageUploader->message('上传成功！', 1);
            }else {
                $imageUploader->message('上传失败！', 0);
            }
        }

        $data = [
            'success' => 0,
            'message' => '请选择文件',
        ];
        exit(json_encode($data, JSON_UNESCAPED_UNICODE));
    }
}